package mitchellcam1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class IpCam 
{
	String cameraID;
	String ipAddress;
	String username;
	String password;
	String httpBasic;
	boolean privacy;
	boolean trigger;
	int port;
	double radius;
	double latitude;
	double longitude;
	int choice;
	int counter;
	int privacyOccupants;
	boolean slow;
	
	public IpCam()
	{
		setCameraID("");
		setIpAddress("");
		setPort(5000);
		setUsername("");
		setPassword("");
		setHttpBasic("");
		setLatitude(0);
		setLongitude(0);
		setRadius(0);
		setPrivacy(true);

		setPrivacyOccupants(0);
	}
	public IpCam(String cameraID)//, String ipAddress, int port, String username, String password, String httpBasic, boolean privacy, boolean trigger, double latitude, double longitude, double radius)
	{
		setCameraID(cameraID);
		setIpAddress(ipAddress);
		setUsername(username);
		setPassword(password);
		setHttpBasic(httpBasic);
		setPrivacy(privacy);

		setPort(port);
		setRadius(radius);
		setLatitude(latitude);
		setLongitude(longitude);
		setPrivacyOccupants(privacyOccupants);
	}
	public boolean setup(String incomingCameraID)
	{
		cameraID = incomingCameraID;
		if (cameraID.equals("null"))
		{
		System.out.println();
		System.out.println("Input Camera ID:");
		Scanner in = new Scanner(System.in);
		cameraID = in.nextLine();
		
		//find out if this is a saved user
		System.out.println("Is this a previous camera you want to import from a text file?");
		System.out.println("1.Yes");
		System.out.println("2.No");
		in = new Scanner(System.in);
		choice = in.nextInt();
		}
		else
		{
			cameraID = incomingCameraID;
			choice = 1;
		}
		
		if (choice == 2)
		{
			try
			{
				//get camera information from user
				System.out.println();
				System.out.println("Input Camera IP address:");
				Scanner in = new Scanner(System.in);
				ipAddress = in.nextLine();
				
				System.out.println();
				System.out.println("Input Camera port:");
				in = new Scanner(System.in);
				port = in.nextInt();
				
//				System.out.println();
//				System.out.println("Input Camera username:");
//				in = new Scanner(System.in);
//				username = in.nextLine();
//				
//				System.out.println();
//				System.out.println("Input Camera password:");
//				in = new Scanner(System.in);
//				password = in.nextLine();
				
				System.out.println();
				System.out.println("Input Camera latitude: (degrees)");
				in = new Scanner(System.in);
				latitude = in.nextDouble();
				
				System.out.println();
				System.out.println("Input Camera longitude (degrees):");
				in = new Scanner(System.in);
				longitude = in.nextDouble();
				
				System.out.println();
				System.out.println("Input Camera radius (meters):");
				in = new Scanner(System.in);
				radius = (.0000106 * in.nextDouble());
				
				System.out.println();
				System.out.println("HTTPbasic encoding not yet implemented. Input HTTPbasic string for camera here:");
				in = new Scanner(System.in);
				httpBasic = in.next();
				
				System.out.println();
				System.out.println("Do you want Mitchellcam to slow down when this camera is private?");
				in = new Scanner(System.in);
				slow = in.nextBoolean();
				
				//write user info to a file
				new File(cameraID + ".camera");
				FileOutputStream outputstream = new FileOutputStream(cameraID +".camera");
				PrintWriter fileWriter = new PrintWriter(outputstream);
				fileWriter.print(ipAddress+"\n");
				fileWriter.print(port+"\n");
				fileWriter.print(httpBasic+"\n");
				fileWriter.print(radius+"\n");
				fileWriter.print(latitude+"\n");
				fileWriter.print(longitude+"\n");
				fileWriter.print(slow+"\n");
				fileWriter.close();
			}
			catch (FileNotFoundException e) 
			{
				e.printStackTrace();
			}
		}
		if (choice == 1)
		{
			//read user info from the appropriate file
			try
			{
			    // Open the file that is the first 
			    // command line parameter
			    FileInputStream fstream = new FileInputStream(cameraID + ".camera");
			    // Get the object of DataInputStream
			    DataInputStream stream = new DataInputStream(fstream);
			    BufferedReader br = new BufferedReader(new InputStreamReader(stream));
			    String strLine;
			    //Read File Line By Line
			    counter = 1;
			    while ((strLine = br.readLine()) != null)   
			    {
			      switch (counter)
			      {
			      	case 1: ipAddress = strLine;
			      			break;
			      			
			      	case 2: port = Integer.parseInt(strLine);
			      			break;
			      			
			      	case 3: httpBasic = strLine;
			      			break;
			      			
			      	case 4: radius = Double.parseDouble(strLine);
			      			break;
			      			
			      	case 5: latitude = Double.parseDouble(strLine);
			      			break;
			      			
			      	case 6: longitude = Double.parseDouble(strLine);
			      			break;
			      			
			      	case 7: slow = Boolean.parseBoolean(strLine);
			      			break;
			      }
			      counter++;
			    }
			    //Close the input stream
			    stream.close();
			   
			}
			catch(Exception e)
			{
				System.out.println("Camera file non-existent or otherwise no-good!");
				e.printStackTrace();
			}
		}
		if (slow == true)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	public void setCameraID(String cameraID)
	{
		this.cameraID = cameraID;
	}
	public void setIpAddress(String ipAddress)
	{
		this.ipAddress = ipAddress;
	}
	public void setPort(int port)
	{
		this.port = port;
	}
	public void setUsername(String username)
	{
		this.username = username;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public void setHttpBasic(String httpBasic)
	{
		this.httpBasic = httpBasic;
	}
	public void setLatitude(double latitude)
	{
		this.latitude = latitude;
	}
	public void setLongitude(double longitude)
	{
		this.longitude = longitude;
	}
	public void setRadius(double radius)
	{
		this.radius = radius;
	}
	public void setPrivacy(boolean privacy)
	{
		this.privacy = privacy;
	}
	public int getPrivacyOccupants() 
	{
		return privacyOccupants;
	}
	public void setPrivacyOccupants(int privacyOccupants) 
	{
		this.privacyOccupants = privacyOccupants;
	}
	public void privacyOn()
	{
		try 
		{
		    System.out.printf("trying to turn on privacy for %s\n", cameraID);
			
			// Create a socket to the host
				Socket socket = new Socket(ipAddress, port);
		    
		    //GET for settings
			    // Send header
			    String path = "cgi-bin/Set?Func=Powerdown&Kind=1&Data=1";
			    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
			    wr.write("GET "+path+" HTTP/1.0\nHost: localhost\nAuthorization: Basic " + httpBasic + "\r\n");
			    wr.write("Content-Length: "+path.length()+"\r\n");
			    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
			    wr.write("\r\n");
			    
			    // Send data
			    wr.write(path);
			    wr.flush(); 
	
//			    // Get response
//			    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//			    String line;
//			    while ((line = rd.readLine()) != null) {
//				    System.out.print(line+"\n");
//
//			    }
			    wr.close();
//			    rd.close();
			  
		} 
			catch (Exception e) {}
	}
	public void privacyOff()
	{
		try {
			System.out.printf("trying to turn off privacy for %s\n", cameraID);
			
			// Create a socket to the host
			    String hostname = ipAddress;
			    Socket socket = new Socket(hostname, port);
//			    System.out.printf("Socket created\n");
		    
		    //GET for settings
			    // Send header
			    String path = "Set?Func=Powerdown&Kind=1&Data=0";
			    BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
			    wr.write("GET "+path+" HTTP/1.0\nHost: localhost\nAuthorization: Basic "+httpBasic+"\r\n");
			    wr.write("Content-Length: "+path.length()+"\r\n");
			    wr.write("Content-Type: application/x-www-form-urlencoded\r\n");
			    wr.write("\r\n");
			    
			    // Send data
			    wr.write(path);
			    wr.flush(); 
	
//			    // Get response
//			    BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
//			    String line;
//			    while ((line = rd.readLine()) != null) {
//			        System.out.print(line);
//			    }
			    wr.close();
//			    rd.close();
		} catch (Exception e) {
		}
	}
	public void controlCamera()
	{
		if (privacyOccupants == 0 && privacy == true)
		 {		
			//turn off privacy 
			privacyOff();
			//mark camera as privacy off
			setPrivacy(false);
		 }
		 
		 if (privacyOccupants != 0 && privacy == false)
		 {
			//turn on privacy 
			privacyOn();
			//mark camera as privacy on
			setPrivacy(true); 
		 }	 
	}

	

	
}
